package org.ofbiz.wvtools;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.DelegatorFactory;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;

import javolution.util.FastList;
import javolution.util.FastMap;

public class CoSecurityServices {
    public static final String module = CoSecurityServices.class.getName();
    public static final String resource = "WvtoolsUiLabels";
    public static final String resourceError = "WvtoolsUiLabels";

    public static List<Map<String, Object>> selectNowUserBySecurityGroup(String userLoginId) {
	Delegator delegator = DelegatorFactory.getDelegator("default");
	List<Map<String, Object>> list = FastList.newInstance();
	try {
	    List<GenericValue> groupList = delegator.findByAnd("UserLoginSecurityGroup",
		    UtilMisc.toMap("userLoginId", userLoginId), null, false);

	    boolean isManage = false; // 管理权限
	    boolean isArea = false; // 大区权限
	    boolean isBu = false; // bu权限
	    boolean isEmployee = false; // 员工权限
	    for (GenericValue group : groupList) {
		String groupId = group.getString("groupId");
		String thruDate = group.getString("thruDate");
		if (groupId.equals("4212") && UtilValidate.isEmpty(thruDate)) {
		    isManage = true;
		} else if (groupId.equals("4213") && UtilValidate.isEmpty(thruDate)) {
		    isArea = true;
		} else if (groupId.equals("4211") && UtilValidate.isEmpty(thruDate)) {
		    isBu = true;
		} else if (groupId.equals("4210") && UtilValidate.isEmpty(thruDate)) {
		    isEmployee = true;
		}
	    }
	    if (userLoginId.equals("13701378791") || userLoginId.equals("13916479883")
		    || userLoginId.equals("13911774478")) {
		isManage = true;
	    }

	    List<GenericValue> areaList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "AREA_DEPARTMENT"), UtilMisc.toList("sequenceId"), false);
	    List<GenericValue> buList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT"), UtilMisc.toList("sequenceId"), false);

	    // 本大区下的人员
	    List<Map<String, Object>> areaPersonList = FastList.newInstance();

	    // 本大区下的各个bu
	    List<GenericValue> shipList = delegator.findByAnd("CoRelationship",
		    UtilMisc.toMap("partyIdTo", userLoginId), null, false);
	    String buName = "";
	    if (UtilValidate.isNotEmpty(shipList)) {
		String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
		if (UtilValidate.isNotEmpty(buCode)) {
		    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
			    UtilMisc.toMap("dictionaryCode", buCode), UtilMisc.toList("sequenceId"), false);
		    GenericValue data = EntityUtil.getFirst(dataList);
		    buName = data.getString("description");
		    List<GenericValue> sunBuList = delegator.findByAnd("DataDictionary",
			    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "parentId", data.getString("parentId")),
			    UtilMisc.toList("sequenceId"), false);
		    x: for (GenericValue bu : sunBuList) {
			String dictionaryCode2 = bu.getString("dictionaryCode2");
			if ("N".equals(dictionaryCode2)) {
			    continue x;
			}

			List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
				UtilMisc.toMap("partyIdFrom", bu.getString("dictionaryCode")),
				UtilMisc.toList("-partyIdTo"), false);
			if (UtilValidate.isNotEmpty(itemShipList)) {
			    for (GenericValue item : itemShipList) {
				Map<String, Object> m = new HashMap<>();
				String userLoginId1 = item.getString("partyIdTo");
				List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
					UtilMisc.toMap("userLoginId", userLoginId1), null, false);
				if (UtilValidate.isNotEmpty(userList)) {
				    m.put("name", EntityUtil.getFirst(userList).getString("name"));
				    m.put("isHide", 0);
				    m.put("value", userLoginId1);
				    if (!areaPersonList.contains(m)) {
					areaPersonList.add(m);
				    }
				}

			    }
			}
		    }
		}
	    }

	    List<Map<String, Object>> buPersonList = FastList.newInstance();

	    // 本BU下的人员
//	    if (UtilValidate.isNotEmpty(shipList)) {
//		String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
//		
//		List<GenericValue> buShipList = delegator.findByAnd("CoRelationship",
//			UtilMisc.toMap("partyIdFrom", buCode), UtilMisc.toList("-partyIdTo"), false);
//
//		if (UtilValidate.isNotEmpty(buShipList)) {
//		    for (GenericValue item : buShipList) {
//			Map<String, Object> m = new HashMap<>();
//			String userLoginId1 = item.getString("partyIdTo");
//
//			List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
//				UtilMisc.toMap("userLoginId", userLoginId1), null, false);
//			if (UtilValidate.isNotEmpty(userList)) {
//			    GenericValue person = EntityUtil.getFirst(userList);
//			    String name = person.getString("name");
//			    if (UtilValidate.isEmpty(name)) {
//				name = userLoginId1;
//			    }
//			    m.put("isHide", 0);
//			    m.put("name", name);
//			    m.put("value", userLoginId1);
//			}
//			if (!buPersonList.contains(m)) {
//			    buPersonList.add(m);
//			}
//		    }
//		}
//	    }

	    List<Map<String, Object>> buPersonList3 = FastList.newInstance();
	    List<GenericValue> buInfoList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
		    UtilMisc.toList("sequenceId"), false);
	    // 本BU下的人员
	    if (UtilValidate.isNotEmpty(buInfoList)) {
		x: for (GenericValue bu : buInfoList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    String buCode = bu.getString("dictionaryCode");
		    String buCode2 = "";
		    if (UtilValidate.isNotEmpty(shipList)) {
			buCode2 = EntityUtil.getFirst(shipList).getString("partyIdFrom");
		    }

		    List<GenericValue> buShipList = delegator.findByAnd("CoRelationship",
			    UtilMisc.toMap("partyIdFrom", buCode), UtilMisc.toList("-partyIdTo"), false);

		    if (UtilValidate.isNotEmpty(buShipList)) {
			for (GenericValue item : buShipList) {
			    Map<String, Object> m = new HashMap<>();
			    String userLoginId1 = item.getString("partyIdTo");

			    List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
				    UtilMisc.toMap("userLoginId", userLoginId1), null, false);
			    if (UtilValidate.isNotEmpty(userList)) {
				GenericValue person = EntityUtil.getFirst(userList);
				String name = person.getString("name");
				if (UtilValidate.isEmpty(name)) {
				    name = userLoginId1;
				}
				m.put("isHide", 0);
				m.put("name", name);
				m.put("value", userLoginId1);
			    }
			    if (!buPersonList3.contains(m)) {
				buPersonList3.add(m);
			    }

			    if (buCode.equals(buCode2)) {
				if (!buPersonList.contains(m)) {
				    buPersonList.add(m);
				}
			    }
			}
		    }
		}
	    }

	    // 普通员工
	    if (isEmployee == true) {
		Map<String, Object> map1 = FastMap.newInstance();
		map1.put("name", "本人");
		map1.put("isHide", 0);
		map1.put("default", "Y");
		map1.put("value", userLoginId);
		list.add(map1);
	    }

	    // bu管理
	    if (isBu == true) {
		list.clear();
		Map<String, Object> map1 = FastMap.newInstance();
		map1.put("name", "本人");
		map1.put("isHide", 0);
		map1.put("value", userLoginId);
		list.add(map1);

		// 判断本BU是否已被禁用
		boolean isShowBu = isShow(delegator, userLoginId, "N");
		if (isShowBu) {
		    Map<String, Object> map2 = FastMap.newInstance();
		    map2.put("name", buName);
		    map2.put("isHide", 0);
		    map2.put("default", "Y");
		    List<String> users = FastList.newInstance();
		    for (Map<String, Object> buPerson : buPersonList) {
			if (!users.contains(buPerson.get("value"))) {
			    users.add((String) buPerson.get("value"));
			}
		    }
		    map2.put("value", "bu," + StringUtils.join(users, ",")); // 获取人员id集合
		    list.add(map2);
		}

		// 担任bu经理的bu列表
		List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
			UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
			UtilMisc.toList("sequenceId"), false);
		int i = 0;
		x: for (GenericValue bu : dataList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    i = i + 1;
		    List<Map<String, Object>> buPersonList2 = FastList.newInstance();
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("isHide", 0);
		    map4.put("name", bu.getString("description"));

		    List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
			    UtilMisc.toMap("partyIdFrom", bu.getString("dictionaryCode")),
			    UtilMisc.toList("-partyIdTo"), false);
		    if (UtilValidate.isNotEmpty(itemShipList)) {
			for (GenericValue item : itemShipList) {
			    Map<String, Object> m = new HashMap<>();
			    m.put("value", item.getString("partyIdTo"));
			    if (!buPersonList2.contains(m)) {
				buPersonList2.add(m);
			    }
			}
		    }

		    List<String> users2 = FastList.newInstance();
		    for (Map<String, Object> areaPerson : buPersonList2) {
			if (!users2.contains(areaPerson.get("value"))) {
			    users2.add((String) areaPerson.get("value"));
			}
		    }
		    if (!isShowBu && i == 1) {
			map4.put("default", "Y");
		    }
		    map4.put("value", "bu," + StringUtils.join(users2, ",")); // 获取人员id集合
		    list.add(map4);
		}

		// 本BU下的人员
		list.addAll(buPersonList3);
	    }
	    // 大区管理
	    if (isArea == true) {
		list.clear();
		Map<String, Object> map1 = FastMap.newInstance();
		map1.put("name", "本人");
		map1.put("isHide", 0);
		map1.put("value", userLoginId);
		list.add(map1);

		// 判断本大区是否已被禁用
		boolean isShowArea = isShow(delegator, userLoginId, "Y");
		if (isShowArea) {
		    Map<String, Object> map3 = FastMap.newInstance();
		    map3.put("name", getBuName(delegator, userLoginId, "Y"));
		    map3.put("isHide", 0);
		    List<String> users = FastList.newInstance();
		    for (Map<String, Object> areaPerson : areaPersonList) {
			if (!users.contains(areaPerson.get("value"))) {
			    users.add((String) areaPerson.get("value"));
			}
		    }
		    map3.put("default", "Y");
		    map3.put("value", "area," + StringUtils.join(users, ",")); // 获取人员id集合
		    list.add(map3);
		}
		// 担任其他大区经理的大区列表
		List<GenericValue> areaList1 = delegator.findByAnd("DataDictionary",
			UtilMisc.toMap("dictionaryTypeId", "AREA_DEPARTMENT", "dictionaryCode3", userLoginId),
			UtilMisc.toList("sequenceId"), false);
		int z = 0;
		y: for (GenericValue area : areaList1) {
		    String dictionaryCode21 = area.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode21)) {
			continue y;
		    }
		    z = z + 1;
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("name", area.getString("description"));
		    map4.put("isHide", 0);
		    // 各大区下的人员
		    List<Map<String, Object>> areaPersonList1 = FastList.newInstance();

		    List<GenericValue> sunBuList = delegator.findByAnd("DataDictionary", UtilMisc
			    .toMap("dictionaryTypeId", "BU_DEPARTMENT", "parentId", area.getString("dictionaryId")),
			    UtilMisc.toList("sequenceId"), false);
		    x: for (GenericValue bu : sunBuList) {
			String dictionaryCode2 = bu.getString("dictionaryCode2");
			if ("N".equals(dictionaryCode2)) {
			    continue x;
			}
			List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
				UtilMisc.toMap("partyIdFrom", bu.getString("dictionaryCode")),
				UtilMisc.toList("-partyIdTo"), false);
			if (UtilValidate.isNotEmpty(itemShipList)) {

			    for (GenericValue item : itemShipList) {
				Map<String, Object> m = new HashMap<>();
				m.put("value", item.getString("partyIdTo"));
				if (!areaPersonList1.contains(m)) {
				    areaPersonList1.add(m);
				}
			    }
			}
		    }
		    List<String> users2 = FastList.newInstance();
		    for (Map<String, Object> areaPerson : areaPersonList1) {
			if (!users2.contains(areaPerson.get("value"))) {
			    users2.add((String) areaPerson.get("value"));
			}
		    }
		    if (!isShowArea && z == 1) {
			map4.put("default", "Y");
		    }
		    map4.put("value", "area," + StringUtils.join(users2, ",")); // 获取人员id集合
		    list.add(map4);
		}

		if (isBu == true) {
		    // 判断本BU是否已被禁用
		    boolean isShowBu = isShow(delegator, userLoginId, "N");
		    if (isShowBu) {
			Map<String, Object> map2 = FastMap.newInstance();
			map2.put("name", buName);
			map2.put("isHide", 0);
			List<String> users = FastList.newInstance();
			for (Map<String, Object> buPerson : buPersonList) {
			    if (!users.contains(buPerson.get("value"))) {
				users.add((String) buPerson.get("value"));
			    }
			}
			map2.put("value", "bu," + StringUtils.join(users, ",")); // 获取人员id集合
			list.add(map2);
		    }

		}

		if (isBu == true) {
		    // 担任bu经理的bu列表
		    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
			    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
			    UtilMisc.toList("sequenceId"), false);
		    int i = 0;
		    x: for (GenericValue bu : dataList) {
			i = i + 1;
			String dictionaryCode2 = bu.getString("dictionaryCode2");
			if ("N".equals(dictionaryCode2)) {
			    continue x;
			}
			List<Map<String, Object>> buPersonList2 = FastList.newInstance();
			Map<String, Object> map4 = FastMap.newInstance();
			map4.put("isHide", 0);
			map4.put("name", bu.getString("description"));

			List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
				UtilMisc.toMap("partyIdFrom", bu.getString("dictionaryCode")),
				UtilMisc.toList("-partyIdTo"), false);
			if (UtilValidate.isNotEmpty(itemShipList)) {
			    for (GenericValue item : itemShipList) {
				Map<String, Object> m = new HashMap<>();
				m.put("value", item.getString("partyIdTo"));
				if (!buPersonList2.contains(m)) {
				    buPersonList2.add(m);
				}
			    }
			}

			List<String> users2 = FastList.newInstance();
			for (Map<String, Object> areaPerson : buPersonList2) {
			    if (!users2.contains(areaPerson.get("value"))) {
				users2.add((String) areaPerson.get("value"));
			    }
			}
			map4.put("value", "bu," + StringUtils.join(users2, ",")); // 获取人员id集合
			list.add(map4);
		    }
		}
		// 本大区下的各个bu
		if (UtilValidate.isNotEmpty(shipList)) {
		    String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
		    if (UtilValidate.isNotEmpty(buCode)) {
			List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
				UtilMisc.toMap("dictionaryCode", buCode), UtilMisc.toList("sequenceId"), false);
			GenericValue data = EntityUtil.getFirst(dataList);

			List<GenericValue> sunBuList = delegator
				.findByAnd(
					"DataDictionary", UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT",
						"parentId", data.getString("parentId")),
					UtilMisc.toList("sequenceId"), false);
			int i = 0;
			x: for (GenericValue bu : sunBuList) {
			    i = i + 1;
			    String dictionaryCode2 = bu.getString("dictionaryCode2");
			    if ("N".equals(dictionaryCode2)) {
				continue x;
			    }
			    List<Map<String, Object>> buPersonList2 = FastList.newInstance();
			    Map<String, Object> map4 = FastMap.newInstance();
			    map4.put("isHide", 0);
			    map4.put("name", bu.getString("description"));

			    List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
				    UtilMisc.toMap("partyIdFrom", bu.getString("dictionaryCode")),
				    UtilMisc.toList("-partyIdTo"), false);
			    if (UtilValidate.isNotEmpty(itemShipList)) {
				for (GenericValue item : itemShipList) {
				    Map<String, Object> m = new HashMap<>();
				    m.put("value", item.getString("partyIdTo"));
				    if (!buPersonList2.contains(m)) {
					buPersonList2.add(m);
				    }
				}
			    }

			    List<String> users2 = FastList.newInstance();
			    for (Map<String, Object> areaPerson : buPersonList2) {
				if (!users2.contains(areaPerson.get("value"))) {
				    users2.add((String) areaPerson.get("value"));
				}
			    }
			    map4.put("value", "bu," + StringUtils.join(users2, ",")); // 获取人员id集合
			    list.add(map4);
			}
		    }
		}
		// 本BU下的人员
		if (isBu == true) {
		    list.addAll(buPersonList3);
		}
		// 本大区下的人员
		list.addAll(areaPersonList);
	    }

	    // 管理者
	    if (isManage == true) {
		list.clear();

		// 所有bu
		List<String> userList = FastList.newInstance();
		x: for (GenericValue bu : buList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
			    UtilMisc.toMap("partyIdFrom", bu.getString("dictionaryCode")),
			    UtilMisc.toList("-partyIdTo"), false);

		    List<String> users = FastList.newInstance();
		    if (UtilValidate.isNotEmpty(itemShipList)) {
			for (GenericValue item : itemShipList) {
			    if (!users.contains(item.getString("partyIdTo"))) {
				users.add(item.getString("partyIdTo"));
			    }
			}
		    }
		    // 去重
		    users = users.stream().distinct().collect(Collectors.toList());
		    userList.addAll(users);
		}
		Map<String, Object> map1 = FastMap.newInstance();
		map1.put("name", "本人");
		map1.put("isHide", 0);
		map1.put("value", userLoginId);
		list.add(map1);

		Map<String, Object> map0 = FastMap.newInstance();
		map0.put("name", "全国");
		map0.put("isHide", 0);
		map0.put("default", "Y");
		map0.put("value", StringUtils.join(userList, ","));
		list.add(map0);

		int i = 0;

		if (isArea == true) { // 如果还有大区管理权限
		    // 判断本大区是否已被禁用
		    boolean isShowArea = isShow(delegator, userLoginId, "Y");
		    if (isShowArea) {
			Map<String, Object> map31 = FastMap.newInstance();
			map31.put("name", getBuName(delegator, userLoginId, "Y"));
			map31.put("isHide", 0);
			List<String> users = FastList.newInstance();
			for (Map<String, Object> areaPerson : areaPersonList) {
			    if (!users.contains(areaPerson.get("value"))) {
				users.add((String) areaPerson.get("value"));
			    }
			}
			map31.put("value", "area," + StringUtils.join(users, ",")); // 获取人员id集合
			list.add(map31);
		    }
		}

		// 各大区
		y: for (GenericValue area : areaList) {
		    String dictionaryCode21 = area.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode21)) {
			continue y;
		    }
		    i = i + 1;
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("name", area.getString("description"));
		    map4.put("isHide", 0);
		    // 各大区下的人员
		    List<Map<String, Object>> areaPersonList1 = FastList.newInstance();

		    List<GenericValue> sunBuList = delegator.findByAnd("DataDictionary", UtilMisc
			    .toMap("dictionaryTypeId", "BU_DEPARTMENT", "parentId", area.getString("dictionaryId")),
			    UtilMisc.toList("sequenceId"), false);
		    x: for (GenericValue bu : sunBuList) {
			String dictionaryCode2 = bu.getString("dictionaryCode2");
			if ("N".equals(dictionaryCode2)) {
			    continue x;
			}
			List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
				UtilMisc.toMap("partyIdFrom", bu.getString("dictionaryCode")),
				UtilMisc.toList("-partyIdTo"), false);
			if (UtilValidate.isNotEmpty(itemShipList)) {

			    for (GenericValue item : itemShipList) {
				Map<String, Object> m = new HashMap<>();
				m.put("value", item.getString("partyIdTo"));
				if (!areaPersonList1.contains(m)) {
				    areaPersonList1.add(m);
				}
			    }
			}
		    }
		    List<String> users = FastList.newInstance();
		    for (Map<String, Object> areaPerson : areaPersonList1) {
			if (!users.contains(areaPerson.get("value"))) {
			    users.add((String) areaPerson.get("value"));
			}
		    }
		    map4.put("value", "area," + StringUtils.join(users, ",")); // 获取人员id集合
		    list.add(map4);
		}
		if (isBu == true) { // 如果还有bu管理权限
		    // 判断本BU是否已被禁用
		    boolean isShowBu = isShow(delegator, userLoginId, "N");
		    if (isShowBu) {
			Map<String, Object> map2 = FastMap.newInstance();
			map2.put("name", buName);
			map2.put("isHide", 0);
			List<String> users = FastList.newInstance();
			for (Map<String, Object> buPerson : buPersonList) {
			    if (!users.contains(buPerson.get("value"))) {
				users.add((String) buPerson.get("value"));
			    }
			}
			map2.put("value", "bu," + StringUtils.join(users, ",")); // 获取人员id集合
			list.add(map2);
		    }
		}

		// 各bu
		x: for (GenericValue bu : buList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    i = i + 1;
		    List<Map<String, Object>> buPersonList1 = FastList.newInstance();
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("name", bu.getString("description"));
		    map4.put("isHide", 0);
		    List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
			    UtilMisc.toMap("partyIdFrom", bu.getString("dictionaryCode")),
			    UtilMisc.toList("-partyIdTo"), false);
		    if (UtilValidate.isNotEmpty(itemShipList)) {
			for (GenericValue item : itemShipList) {
			    Map<String, Object> m = new HashMap<>();
			    m.put("value", item.getString("partyIdTo"));
			    if (!buPersonList1.contains(m)) {
				buPersonList1.add(m);
			    }
			}

		    }
		    List<String> users = FastList.newInstance();
		    for (Map<String, Object> areaPerson : buPersonList1) {
			if (!users.contains(areaPerson.get("value"))) {
			    users.add((String) areaPerson.get("value"));
			}
		    }
		    map4.put("value", "bu," + StringUtils.join(users, ",")); // 获取人员id集合
		    list.add(map4);
		}
		// 本BU下的人员
		if (isBu == true) { // 如果有bu管理权限
		    list.addAll(buPersonList3);
		}
		if (isArea == true) { // 如果有大区管理权限
		    // 本大区下的人员
		    list.addAll(areaPersonList);
		}
	    }
	    if (UtilValidate.isNotEmpty(list)) {
		list = delRepeat(list); // 去重
	    }
	} catch (GenericEntityException e) {
	    e.printStackTrace();
	}
	return list;
    }

    public static Map<String, Object> listAllBu(DispatchContext ctx, Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();

	List<Map<String, Object>> nfsList = FastList.newInstance();
	try {

	    // bu列表
	    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT"), UtilMisc.toList("sequenceId"), false);
	    x: for (GenericValue bu : dataList) {
		String dictionaryCode2 = bu.getString("dictionaryCode2");
		if ("N".equals(dictionaryCode2)) {
		    continue x;
		}
		Map<String, Object> map4 = FastMap.newInstance();
		map4.put("title", bu.getString("description"));
		map4.put("key", bu.getString("dictionaryCode"));
		map4.put("text", bu.getString("description"));
		map4.put("value", bu.getString("dictionaryCode"));
		nfsList.add(map4);
	    }

	    nfsList = nfsList.stream().distinct().collect(Collectors.toList());
	    result.put("nfsList", nfsList);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError(e.getMessage());
	}
	return result;
    }

    public static Map<String, Object> listUserBu(DispatchContext ctx, Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	GenericValue userLogin = (GenericValue) context.get("userLogin");

	List<Map<String, Object>> nfsList = FastList.newInstance();
	try {
	    String userLoginId = userLogin.getString("userLoginId");

	    // 判断本大区是否已被禁用
	    boolean isShowBu = isShow(delegator, userLoginId, "N");
	    if (isShowBu) {
		Map<String, Object> map2 = FastMap.newInstance();
		String buCode = getBu(delegator, userLoginId, "N");
		if (UtilValidate.isNotEmpty(buCode)) {
		    map2.put("title", getBuName(delegator, userLoginId, "N"));
		    map2.put("key", buCode);
		    map2.put("text", getBuName(delegator, userLoginId, "N"));
		    map2.put("value", buCode);
		    nfsList.add(map2);
		}
	    }
	    // 担任bu经理的bu列表
	    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
		    UtilMisc.toList("sequenceId"), false);
	    x: for (GenericValue bu : dataList) {
		String dictionaryCode2 = bu.getString("dictionaryCode2");
		if ("N".equals(dictionaryCode2)) {
		    continue x;
		}
		Map<String, Object> map4 = FastMap.newInstance();
		map4.put("title", bu.getString("description"));
		map4.put("key", bu.getString("dictionaryCode"));
		map4.put("text", bu.getString("description"));
		map4.put("value", bu.getString("dictionaryCode"));
		nfsList.add(map4);
	    }

	    // 担任大区经理的bu列表
	    List<GenericValue> dataList1 = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "AREA_DEPARTMENT", "dictionaryCode3", userLoginId),
		    UtilMisc.toList("sequenceId"), false);
	    x: for (GenericValue area : dataList1) {
		String dictionaryCode2 = area.getString("dictionaryCode2");
		if ("N".equals(dictionaryCode2)) {
		    continue x;
		}
		List<GenericValue> buList = delegator.findByAnd("DataDictionary",
			UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "parentId", area.getString("dictionaryId")),
			UtilMisc.toList("sequenceId"), false);
		y: for (GenericValue bu : buList) {
		    String dictionaryCode22 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode22)) {
			continue y;
		    }
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("title", bu.getString("description"));
		    map4.put("key", bu.getString("dictionaryCode"));
		    map4.put("text", bu.getString("description"));
		    map4.put("value", bu.getString("dictionaryCode"));
		    nfsList.add(map4);
		}
	    }

	    nfsList = nfsList.stream().distinct().collect(Collectors.toList());
	    result.put("nfsList", nfsList);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError(e.getMessage());
	}
	return result;
    }

    public static Map<String, Object> selectNowUserBySecurityGroup(DispatchContext ctx, Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	try {
	    String userLoginId = userLogin.getString("userLoginId");
	    List<GenericValue> groupList = delegator.findByAnd("UserLoginSecurityGroup",
		    UtilMisc.toMap("userLoginId", userLoginId), null, false);

	    boolean isManage = false; // 管理权限
	    boolean isArea = false; // 大区权限
	    boolean isBu = false; // bu权限
	    boolean isEmployee = false; // 员工权限
	    for (GenericValue group : groupList) {
		String groupId = group.getString("groupId");
		String thruDate = group.getString("thruDate");
		if (groupId.equals("4212") && UtilValidate.isEmpty(thruDate)) {
		    isManage = true;
		} else if (groupId.equals("4213") && UtilValidate.isEmpty(thruDate)) {
		    isArea = true;
		} else if (groupId.equals("4211") && UtilValidate.isEmpty(thruDate)) {
		    isBu = true;
		} else if (groupId.equals("4210") && UtilValidate.isEmpty(thruDate)) {
		    isEmployee = true;
		}
	    }
	    if (userLoginId.equals("13701378791") || userLoginId.equals("13916479883")
		    || userLoginId.equals("13911774478")) {
		isManage = true;
	    }

	    List<GenericValue> areaList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "AREA_DEPARTMENT"), UtilMisc.toList("sequenceId"), false);
	    List<GenericValue> buList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT"), UtilMisc.toList("sequenceId"), false);

	    List<GenericValue> shipList = delegator.findByAnd("CoRelationship",
		    UtilMisc.toMap("partyIdTo", userLoginId), null, false);

	    List<Map<String, Object>> buPersonList = FastList.newInstance();

	    // 本bu下的人员
//	    if (UtilValidate.isNotEmpty(shipList)) {
//		String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
//		List<GenericValue> buShipList = delegator.findByAnd("CoRelationship",
//			UtilMisc.toMap("partyIdFrom", buCode), UtilMisc.toList("-partyIdTo"), false);
//
//		if (UtilValidate.isNotEmpty(buShipList)) {
//		    for (GenericValue item : buShipList) {
//			Map<String, Object> m = new HashMap<>();
//			String userLoginId1 = item.getString("partyIdTo");
//
//			List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
//				UtilMisc.toMap("userLoginId", userLoginId1), null, false);
//			if (UtilValidate.isNotEmpty(userList)) {
//			    GenericValue person = EntityUtil.getFirst(userList);
//			    String name = person.getString("name");
//			    if (UtilValidate.isEmpty(name)) {
//				name = userLoginId1;
//			    }
//			    m.put("label", name);
//			    m.put("value", userLoginId1);
//
//			    if (!buPersonList.contains(m)) {
//				buPersonList.add(m);
//			    }
//			}
//		    }
//		}
//	    }
	    List<Map<String, Object>> buPersonList3 = FastList.newInstance();
	    List<GenericValue> buInfoList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
		    UtilMisc.toList("sequenceId"), false);
	    // 本BU下的人员
	    if (UtilValidate.isNotEmpty(buInfoList)) {
		x: for (GenericValue bu : buInfoList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    String buCode = bu.getString("dictionaryCode");
		    String buCode2 = "";
		    if (UtilValidate.isNotEmpty(shipList)) {
			buCode2 = EntityUtil.getFirst(shipList).getString("partyIdFrom");
		    }

		    List<GenericValue> buShipList = delegator.findByAnd("CoRelationship",
			    UtilMisc.toMap("partyIdFrom", buCode), UtilMisc.toList("-partyIdTo"), false);

		    if (UtilValidate.isNotEmpty(buShipList)) {
			for (GenericValue item : buShipList) {
			    Map<String, Object> m = new HashMap<>();
			    String userLoginId1 = item.getString("partyIdTo");

			    List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
				    UtilMisc.toMap("userLoginId", userLoginId1), null, false);
			    if (UtilValidate.isNotEmpty(userList)) {
				GenericValue person = EntityUtil.getFirst(userList);
				String name = person.getString("name");
				if (UtilValidate.isEmpty(name)) {
				    name = userLoginId1;
				}
				m.put("label", name);
				m.put("value", userLoginId1);

				if (!buPersonList3.contains(m)) {
				    buPersonList3.add(m);
				}
				if (buCode.equals(buCode2)) {
				    if (!buPersonList.contains(m)) {
					buPersonList.add(m);
				    }
				}
			    }
			}
		    }
		}
	    }

	    // 普通员工
	    if (isEmployee == true) {
		List<Map<String, Object>> list = FastList.newInstance();
		Map<String, Object> map1 = FastMap.newInstance();
		map1.put("label", "本人");
		map1.put("value", userLoginId);
		map1.put("default", "Y");
		list.add(map1);
		// 判断本大区是否已被禁用
		boolean isShowBu = isShow(delegator, userLoginId, "N");
		if (isShowBu) {
		    Map<String, Object> map2 = FastMap.newInstance();
		    map2.put("label", getBuName(delegator, userLoginId, "N"));
		    map2.put("value", getBu(delegator, userLoginId, "N"));
		    list.add(map2);
		}

		result.put("list", list);
	    }
	    // bu管理
	    if (isBu == true) {
		List<Map<String, Object>> list = FastList.newInstance();
		Map<String, Object> map1 = FastMap.newInstance();
		map1.put("label", "本人");
		map1.put("value", userLoginId);
		list.add(map1);
		// 全国
		Map<String, Object> map5 = FastMap.newInstance();
		map5.put("label", "全国");
		map5.put("value", "allData");
		list.add(map5);
		// 判断本大区是否已被禁用
		boolean isShowBu = isShow(delegator, userLoginId, "N");
		if (isShowBu) {
		    Map<String, Object> map2 = FastMap.newInstance();
		    map2.put("label", getBuName(delegator, userLoginId, "N"));
		    map2.put("value", getBu(delegator, userLoginId, "N"));
		    map2.put("default", "Y");
		    list.add(map2);
		}

		// 担任bu经理的bu列表
		List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
			UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
			UtilMisc.toList("sequenceId"), false);
		int i = 0;
		x: for (GenericValue bu : dataList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    i = i + 1;
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", bu.getString("description"));
		    map4.put("value", bu.getString("dictionaryCode"));
		    if (!isShowBu && i == 1) {
			map4.put("default", "Y");
		    }
		    list.add(map4);
		}

		// 本BU下的人员
		list.addAll(buPersonList3);
		list = delRepeat(list);
		result.put("list", list);
	    }

	    // 大区管理
	    if (isArea == true) {

		List<Map<String, Object>> list = FastList.newInstance();
		Map<String, Object> map1 = FastMap.newInstance();
		map1.put("label", "本人");
		map1.put("value", userLoginId);
		list.add(map1);
		// 全国
		Map<String, Object> map5 = FastMap.newInstance();
		map5.put("label", "全国");
		map5.put("value", "allData");
		list.add(map5);

		// 判断本大区是否已被禁用
		boolean isShowArea = isShow(delegator, userLoginId, "Y");
		if (isShowArea) {
		    Map<String, Object> map3 = FastMap.newInstance();
		    map3.put("label", getBuName(delegator, userLoginId, "Y"));
		    map3.put("value", getBu(delegator, userLoginId, "Y"));
		    map3.put("default", "Y");
		    list.add(map3);
		}
		// 担任大区经理的大区列表
		List<GenericValue> dataList1 = delegator.findByAnd("DataDictionary",
			UtilMisc.toMap("dictionaryTypeId", "AREA_DEPARTMENT", "dictionaryCode3", userLoginId),
			UtilMisc.toList("sequenceId"), false);
		int i = 0;
		x: for (GenericValue area : dataList1) {
		    String dictionaryCode2 = area.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    i = i + 1;
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", area.getString("description"));
		    map4.put("value", area.getString("dictionaryCode"));
		    if (!isShowArea && i == 1) {
			map4.put("default", "Y");
		    }
		    list.add(map4);
		}

		if (isBu == true) {
		    // 判断本大区是否已被禁用
		    boolean isShowBu = isShow(delegator, userLoginId, "N");
		    if (isShowBu) {
			Map<String, Object> map2 = FastMap.newInstance();
			map2.put("label", getBuName(delegator, userLoginId, "N"));
			map2.put("value", getBu(delegator, userLoginId, "N"));
			list.add(map2);
		    }
		    // 担任bu经理的bu列表
		    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
			    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
			    UtilMisc.toList("sequenceId"), false);
		    int z = 0;
		    x: for (GenericValue bu : dataList) {
			z = z + 1;
			String dictionaryCode2 = bu.getString("dictionaryCode2");
			if ("N".equals(dictionaryCode2)) {
			    continue x;
			}
			Map<String, Object> map4 = FastMap.newInstance();
			map4.put("label", bu.getString("description"));
			map4.put("value", bu.getString("dictionaryCode"));
			list.add(map4);
		    }
		}

		// 本大区下的各个bu
		if (UtilValidate.isNotEmpty(shipList)) {
		    String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
		    if (UtilValidate.isNotEmpty(buCode)) {
			List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
				UtilMisc.toMap("dictionaryCode", buCode), UtilMisc.toList("sequenceId"), false);
			GenericValue data = EntityUtil.getFirst(dataList);
			List<GenericValue> sunBuList = delegator
				.findByAnd(
					"DataDictionary", UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT",
						"parentId", data.getString("parentId")),
					UtilMisc.toList("sequenceId"), false);
			x: for (GenericValue bu : sunBuList) {
			    String dictionaryCode2 = bu.getString("dictionaryCode2");
			    if ("N".equals(dictionaryCode2)) {
				continue x;
			    }
			    Map<String, Object> map4 = FastMap.newInstance();
			    map4.put("label", bu.getString("description"));
			    map4.put("value", bu.getString("dictionaryCode"));
			    list.add(map4);
			}
		    }
		}

		// 本大区下的人员
		List<Map<String, Object>> areaPersonList = FastList.newInstance();
		if (UtilValidate.isNotEmpty(shipList)) {
		    String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
		    if (UtilValidate.isNotEmpty(buCode)) {
			List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
				UtilMisc.toMap("dictionaryCode", buCode), UtilMisc.toList("sequenceId"), false);
			GenericValue data = EntityUtil.getFirst(dataList);
			List<GenericValue> sunBuList = delegator
				.findByAnd(
					"DataDictionary", UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT",
						"parentId", data.getString("parentId")),
					UtilMisc.toList("sequenceId"), false);
			x: for (GenericValue bu : sunBuList) {
			    String dictionaryCode2 = bu.getString("dictionaryCode2");
			    if ("N".equals(dictionaryCode2)) {
				continue x;
			    }
			    List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
				    UtilMisc.toMap("partyIdFrom", bu.getString("dictionaryCode")),
				    UtilMisc.toList("-partyIdTo"), false);
			    if (UtilValidate.isNotEmpty(itemShipList)) {
				for (GenericValue item : itemShipList) {
				    Map<String, Object> m = new HashMap<>();
				    String userLoginId1 = item.getString("partyIdTo");
				    List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
					    UtilMisc.toMap("userLoginId", userLoginId1), null, false);
				    if (UtilValidate.isNotEmpty(userList)) {
					m.put("label", EntityUtil.getFirst(userList).getString("name"));
					m.put("value", userLoginId1);
					if (!areaPersonList.contains(m)) {
					    areaPersonList.add(m);
					}
				    }

				}
			    }
			}
		    }
		    // 本大区下的人员
		    list.addAll(areaPersonList);
		}

		// 本BU下的人员
		if (isBu == true) {
		    list.addAll(buPersonList3);
		}
		list = delRepeat(list);
		result.put("list", list);
	    }

	    // 管理者
	    if (isManage == true) {
		List<Map<String, Object>> list = FastList.newInstance();
		Map<String, Object> map1 = FastMap.newInstance();
		map1.put("label", "本人");
		map1.put("value", userLoginId);
		list.add(map1);

		Map<String, Object> map3 = FastMap.newInstance();
		map3.put("label", "全国");
		map3.put("value", "allData");
		map3.put("default", "Y");
		list.add(map3);

		if (isArea == true) {
		    // 判断本大区是否已被禁用
		    boolean isShowArea = isShow(delegator, userLoginId, "Y");
		    if (isShowArea) {
			Map<String, Object> map31 = FastMap.newInstance();
			map31.put("label", getBuName(delegator, userLoginId, "Y"));
			map31.put("value", getBu(delegator, userLoginId, "Y"));
			list.add(map31);
		    }
		}

		// 各大区
		x: for (GenericValue area : areaList) {
		    String dictionaryCode2 = area.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", area.getString("description"));
		    map4.put("value", area.getString("dictionaryCode"));
		    list.add(map4);
		}

		if (isBu == true) {
		    // 判断本大区是否已被禁用
		    boolean isShowBu = isShow(delegator, userLoginId, "N");
		    if (isShowBu) {
			Map<String, Object> map2 = FastMap.newInstance();
			map2.put("label", getBuName(delegator, userLoginId, "N"));
			map2.put("value", getBu(delegator, userLoginId, "N"));
			list.add(map2);
		    }
		}

		// 各bu
		x: for (GenericValue bu : buList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", bu.getString("description"));
		    map4.put("value", bu.getString("dictionaryCode"));
		    list.add(map4);
		}

		if (isArea == true) {
		    // 本大区下的人员
		    List<Map<String, Object>> areaPersonList = FastList.newInstance();
		    // 本大区下的各个bu

		    if (UtilValidate.isNotEmpty(shipList)) {
			String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
			if (UtilValidate.isNotEmpty(buCode)) {
			    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
				    UtilMisc.toMap("dictionaryCode", buCode), UtilMisc.toList("sequenceId"), false);
			    GenericValue data = EntityUtil.getFirst(dataList);

			    List<GenericValue> sunBuList = delegator
				    .findByAnd(
					    "DataDictionary", UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT",
						    "parentId", data.getString("parentId")),
					    UtilMisc.toList("sequenceId"), false);
			    x: for (GenericValue bu : sunBuList) {
				String dictionaryCode2 = bu.getString("dictionaryCode2");
				if ("N".equals(dictionaryCode2)) {
				    continue x;
				}
				List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
					UtilMisc.toMap("partyIdFrom", bu.getString("dictionaryCode")),
					UtilMisc.toList("-partyIdTo"), false);
				if (UtilValidate.isNotEmpty(itemShipList)) {
				    for (GenericValue item : itemShipList) {
					Map<String, Object> m = new HashMap<>();
					String userLoginId1 = item.getString("partyIdTo");
					List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
						UtilMisc.toMap("userLoginId", userLoginId1), null, false);

					if (UtilValidate.isNotEmpty(userList)) {
					    m.put("name", EntityUtil.getFirst(userList).getString("name"));
					    m.put("isHide", 0);
					    m.put("value", userLoginId1);
					    if (!areaPersonList.contains(m)) {
						areaPersonList.add(m);
					    }
					}
				    }
				}
			    }
			}
			// 本大区下的人员
			list.addAll(areaPersonList);
		    }
		}

		// 本BU下的人员
		if (isBu == true) {
		    list.addAll(buPersonList3);
		}

		list = delRepeat(list);
		result.put("list", list);
	    }
	    if (UtilValidate.isEmpty(result.get("list"))) {
		List<Map<String, Object>> list = FastList.newInstance();
		Map<String, Object> map1 = FastMap.newInstance();
		map1.put("label", "本人");
		map1.put("value", userLoginId);
		list.add(map1);

		result.put("list", list);
	    }
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError(e.getMessage());
	}
	return result;
    }

    public static Map<String, Object> selectNowUserBySecurityGroupBu(DispatchContext ctx, Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	try {
	    String userLoginId = userLogin.getString("userLoginId");
	    List<GenericValue> groupList = delegator.findByAnd("UserLoginSecurityGroup",
		    UtilMisc.toMap("userLoginId", userLoginId), null, false);

	    boolean isManage = false; // 管理权限
	    boolean isArea = false; // 大区权限
	    boolean isBu = false; // bu权限
	    boolean isEmployee = false; // 员工权限
	    for (GenericValue group : groupList) {
		String groupId = group.getString("groupId");
		String thruDate = group.getString("thruDate");
		if (groupId.equals("4212") && UtilValidate.isEmpty(thruDate)) {
		    isManage = true;
		} else if (groupId.equals("4213") && UtilValidate.isEmpty(thruDate)) {
		    isArea = true;
		} else if (groupId.equals("4211") && UtilValidate.isEmpty(thruDate)) {
		    isBu = true;
		} else if (groupId.equals("4210") && UtilValidate.isEmpty(thruDate)) {
		    isEmployee = true;
		}
	    }
	    if (userLoginId.equals("13701378791") || userLoginId.equals("13916479883")
		    || userLoginId.equals("13911774478")) {
		isManage = true;
	    }

	    List<GenericValue> areaList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "AREA_DEPARTMENT"), UtilMisc.toList("sequenceId"), false);
	    List<GenericValue> buList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT"), UtilMisc.toList("sequenceId"), false);

	    List<GenericValue> shipList = delegator.findByAnd("CoRelationship",
		    UtilMisc.toMap("partyIdTo", userLoginId), null, false);

//	    List<Map<String, Object>> buPersonList = FastList.newInstance();
//	    // 本bu下的人员
////	    if (UtilValidate.isNotEmpty(shipList)) {
////		String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
////		
////		List<GenericValue> buShipList = delegator.findByAnd("CoRelationship",
////			UtilMisc.toMap("partyIdFrom", buCode), UtilMisc.toList("-partyIdTo"), false);
////
////		if (UtilValidate.isNotEmpty(buShipList)) {
////		    for (GenericValue item : buShipList) {
////			Map<String, Object> m = new HashMap<>();
////			String userLoginId1 = item.getString("partyIdTo");
////
////			List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
////				UtilMisc.toMap("userLoginId", userLoginId1), null, false);
////			if (UtilValidate.isNotEmpty(userList)) {
////			    GenericValue person = EntityUtil.getFirst(userList);
////			    String name = person.getString("name");
////			    if (UtilValidate.isEmpty(name)) {
////				name = userLoginId1;
////			    }
////			    m.put("label", name);
////			    m.put("value", userLoginId1);
////
////			    if (!buPersonList.contains(m)) {
////				buPersonList.add(m);
////			    }
////			}
////		    }
////		}
////	    }
//	    List<Map<String, Object>> buPersonList3 = FastList.newInstance();
//	    List<GenericValue> buInfoList = delegator.findByAnd("DataDictionary",
//		    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
//		    UtilMisc.toList("sequenceId"), false);
//	    // 本BU下的人员
//	    if (UtilValidate.isNotEmpty(buInfoList)) {
//		x: for (GenericValue bu : buInfoList) {
//		    String dictionaryCode2 = bu.getString("dictionaryCode2");
//		    if ("N".equals(dictionaryCode2)) {
//			continue x;
//		    }
//		    String buCode = bu.getString("dictionaryCode");
//		    String buCode2 = "";
//		    if (UtilValidate.isNotEmpty(shipList)) {
//			buCode2 = EntityUtil.getFirst(shipList).getString("partyIdFrom");
//		    }
//		    List<GenericValue> buShipList = delegator.findByAnd("CoRelationship",
//			    UtilMisc.toMap("partyIdFrom", buCode), UtilMisc.toList("-partyIdTo"), false);
//
//		    if (UtilValidate.isNotEmpty(buShipList)) {
//			for (GenericValue item : buShipList) {
//			    Map<String, Object> m = new HashMap<>();
//			    String userLoginId1 = item.getString("partyIdTo");
//
//			    List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
//				    UtilMisc.toMap("userLoginId", userLoginId1), null, false);
//			    if (UtilValidate.isNotEmpty(userList)) {
//				GenericValue person = EntityUtil.getFirst(userList);
//				String name = person.getString("name");
//				if (UtilValidate.isEmpty(name)) {
//				    name = userLoginId1;
//				}
//				m.put("label", name);
//				m.put("value", userLoginId1);
//
//				if (!buPersonList3.contains(m)) {
//				    buPersonList3.add(m);
//				}
//				if (buCode.equals(buCode2)) {
//				    if (!buPersonList.contains(m)) {
//					buPersonList.add(m);
//				    }
//				}
//			    }
//			}
//		    }
//		}
//	    }

	    // 普通员工
	    if (isEmployee == true) {
		List<Map<String, Object>> list = FastList.newInstance();
		// 判断本大区是否已被禁用
		boolean isShowBu = isShow(delegator, userLoginId, "N");
		if (isShowBu) {
		    Map<String, Object> map2 = FastMap.newInstance();
		    map2.put("label", getBuName(delegator, userLoginId, "N"));
		    map2.put("value", getBu(delegator, userLoginId, "N"));
		    map2.put("default", "Y");
		    list.add(map2);
		}
		result.put("list", list);
	    }
	    // bu管理
	    if (isBu == true) {
		List<Map<String, Object>> list = FastList.newInstance();
		// 全国
		Map<String, Object> map5 = FastMap.newInstance();
		map5.put("label", "全国");
		map5.put("value", "allData");
		list.add(map5);
		// 判断本大区是否已被禁用
		boolean isShowBu = isShow(delegator, userLoginId, "N");
		if (isShowBu) {
		    Map<String, Object> map2 = FastMap.newInstance();
		    map2.put("label", getBuName(delegator, userLoginId, "N"));
		    map2.put("value", getBu(delegator, userLoginId, "N"));
		    map2.put("default", "Y");
		    list.add(map2);
		}

		// 担任bu经理的bu列表
		List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
			UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
			UtilMisc.toList("sequenceId"), false);
		int i = 0;
		x: for (GenericValue bu : dataList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    i = i + 1;
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", bu.getString("description"));
		    map4.put("value", bu.getString("dictionaryCode"));
		    if (!isShowBu && i == 1) {
			map4.put("default", "Y");
		    }
		    list.add(map4);
		}
		list = delRepeat(list);
		result.put("list", list);
	    }

	    // 大区管理
	    if (isArea == true) {
		List<Map<String, Object>> list = FastList.newInstance();
		// 全国
		Map<String, Object> map5 = FastMap.newInstance();
		map5.put("label", "全国");
		map5.put("value", "allData");
		list.add(map5);

		// 判断本大区是否已被禁用
		boolean isShowArea = isShow(delegator, userLoginId, "Y");
		if (isShowArea) {
		    Map<String, Object> map3 = FastMap.newInstance();
		    map3.put("label", getBuName(delegator, userLoginId, "Y"));
		    map3.put("value", getBu(delegator, userLoginId, "Y"));
		    map3.put("default", "Y");
		    list.add(map3);
		}
		// 担任大区经理的大区列表
		List<GenericValue> dataList1 = delegator.findByAnd("DataDictionary",
			UtilMisc.toMap("dictionaryTypeId", "AREA_DEPARTMENT", "dictionaryCode3", userLoginId),
			UtilMisc.toList("sequenceId"), false);
		int i = 0;
		x: for (GenericValue area : dataList1) {
		    String dictionaryCode2 = area.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    i = i + 1;
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", area.getString("description"));
		    map4.put("value", area.getString("dictionaryCode"));
		    if (!isShowArea && i == 1) {
			map4.put("default", "Y");
		    }

		    list.add(map4);
		}

		if (isBu == true) {
		    // 判断本大区是否已被禁用
		    boolean isShowBu = isShow(delegator, userLoginId, "N");
		    if (isShowBu) {
			Map<String, Object> map2 = FastMap.newInstance();
			map2.put("label", getBuName(delegator, userLoginId, "N"));
			map2.put("value", getBu(delegator, userLoginId, "N"));
			list.add(map2);
		    }
		    // 担任bu经理的bu列表
		    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
			    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
			    UtilMisc.toList("sequenceId"), false);
		    int z = 0;
		    x: for (GenericValue bu : dataList) {
			z = z + 1;
			String dictionaryCode2 = bu.getString("dictionaryCode2");
			if ("N".equals(dictionaryCode2)) {
			    continue x;
			}
			Map<String, Object> map4 = FastMap.newInstance();
			map4.put("label", bu.getString("description"));
			map4.put("value", bu.getString("dictionaryCode"));
			list.add(map4);
		    }
		}

		// 本大区下的各个bu
		if (UtilValidate.isNotEmpty(shipList)) {
		    String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
		    if (UtilValidate.isNotEmpty(buCode)) {
			List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
				UtilMisc.toMap("dictionaryCode", buCode), UtilMisc.toList("sequenceId"), false);
			GenericValue data = EntityUtil.getFirst(dataList);
			List<GenericValue> sunBuList = delegator
				.findByAnd(
					"DataDictionary", UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT",
						"parentId", data.getString("parentId")),
					UtilMisc.toList("sequenceId"), false);
			x: for (GenericValue bu : sunBuList) {
			    String dictionaryCode2 = bu.getString("dictionaryCode2");
			    if ("N".equals(dictionaryCode2)) {
				continue x;
			    }
			    Map<String, Object> map4 = FastMap.newInstance();
			    map4.put("label", bu.getString("description"));
			    map4.put("value", bu.getString("dictionaryCode"));
			    list.add(map4);
			}
		    }
		}
		list = delRepeat(list);
		result.put("list", list);
	    }

	    // 管理者
	    if (isManage == true) {
		List<Map<String, Object>> list = FastList.newInstance();

		Map<String, Object> map3 = FastMap.newInstance();
		map3.put("label", "全国");
		map3.put("value", "allData");
		map3.put("default", "Y");
		list.add(map3);

		if (isArea == true) {
		    // 判断本大区是否已被禁用
		    boolean isShowArea = isShow(delegator, userLoginId, "Y");
		    if (isShowArea) {
			Map<String, Object> map31 = FastMap.newInstance();
			map31.put("label", getBuName(delegator, userLoginId, "Y"));
			map31.put("value", getBu(delegator, userLoginId, "Y"));
			list.add(map31);
		    }
		}
		// 各大区
		x: for (GenericValue area : areaList) {
		    String dictionaryCode2 = area.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", area.getString("description"));
		    map4.put("value", area.getString("dictionaryCode"));
		    list.add(map4);
		}
		if (isBu == true) {
		    // 判断本大区是否已被禁用
		    boolean isShowBu = isShow(delegator, userLoginId, "N");
		    if (isShowBu) {
			Map<String, Object> map2 = FastMap.newInstance();
			map2.put("label", getBuName(delegator, userLoginId, "N"));
			map2.put("value", getBu(delegator, userLoginId, "N"));
			list.add(map2);
		    }
		}

		// 各bu
		x: for (GenericValue bu : buList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", bu.getString("description"));
		    map4.put("value", bu.getString("dictionaryCode"));
		    list.add(map4);
		}
		list = delRepeat(list);
		result.put("list", list);
	    }
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError(e.getMessage());
	}
	return result;
    }

    public static Map<String, Object> selectNowUserBySecurityGroupBu2(DispatchContext ctx,
	    Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	try {
	    String userLoginId = userLogin.getString("userLoginId");
	    List<GenericValue> groupList = delegator.findByAnd("UserLoginSecurityGroup",
		    UtilMisc.toMap("userLoginId", userLoginId), null, false);

	    boolean isManage = false; // 管理权限
	    boolean isArea = false; // 大区权限
	    boolean isBu = false; // bu权限
	    boolean isEmployee = false; // 员工权限
	    for (GenericValue group : groupList) {
		String groupId = group.getString("groupId");
		String thruDate = group.getString("thruDate");
		if (groupId.equals("4212") && UtilValidate.isEmpty(thruDate)) {
		    isManage = true;
		} else if (groupId.equals("4213") && UtilValidate.isEmpty(thruDate)) {
		    isArea = true;
		} else if (groupId.equals("4211") && UtilValidate.isEmpty(thruDate)) {
		    isBu = true;
		} else if (groupId.equals("4210") && UtilValidate.isEmpty(thruDate)) {
		    isEmployee = true;
		}
	    }
	    if (userLoginId.equals("13701378791") || userLoginId.equals("13916479883")
		    || userLoginId.equals("13911774478")) {
		isManage = true;
	    }

	    List<GenericValue> areaList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "AREA_DEPARTMENT"), UtilMisc.toList("sequenceId"), false);
	    List<GenericValue> buList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT"), UtilMisc.toList("sequenceId"), false);

	    List<GenericValue> shipList = delegator.findByAnd("CoRelationship",
		    UtilMisc.toMap("partyIdTo", userLoginId), null, false);

//	    List<Map<String, Object>> buPersonList = FastList.newInstance();
//
//	    // 本bu下的人员
////	    if (UtilValidate.isNotEmpty(shipList)) {
////		String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
////		List<GenericValue> buShipList = delegator.findByAnd("CoRelationship",
////			UtilMisc.toMap("partyIdFrom", buCode), UtilMisc.toList("-partyIdTo"), false);
////
////		if (UtilValidate.isNotEmpty(buShipList)) {
////		    for (GenericValue item : buShipList) {
////			Map<String, Object> m = new HashMap<>();
////			String userLoginId1 = item.getString("partyIdTo");
////
////			List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
////				UtilMisc.toMap("userLoginId", userLoginId1), null, false);
////			if (UtilValidate.isNotEmpty(userList)) {
////			    GenericValue person = EntityUtil.getFirst(userList);
////			    String name = person.getString("name");
////			    if (UtilValidate.isEmpty(name)) {
////				name = userLoginId1;
////			    }
////			    m.put("label", name);
////			    m.put("value", userLoginId1);
////
////			    if (!buPersonList.contains(m)) {
////				buPersonList.add(m);
////			    }
////			}
////		    }
////		}
////	    }
//
//	    // 本BU下的人员
//	    List<Map<String, Object>> buPersonList3 = FastList.newInstance();
//	    List<GenericValue> buInfoList = delegator.findByAnd("DataDictionary",
//		    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
//		    UtilMisc.toList("sequenceId"), false);
//	    // 本BU下的人员
//	    if (UtilValidate.isNotEmpty(buInfoList)) {
//		x: for (GenericValue bu : buInfoList) {
//		    String dictionaryCode2 = bu.getString("dictionaryCode2");
//		    if ("N".equals(dictionaryCode2)) {
//			continue x;
//		    }
//		    String buCode = bu.getString("dictionaryCode");
//		    String buCode2 = "";
//		    if (UtilValidate.isNotEmpty(shipList)) {
//			buCode2 = EntityUtil.getFirst(shipList).getString("partyIdFrom");
//		    }
//		    List<GenericValue> buShipList = delegator.findByAnd("CoRelationship",
//			    UtilMisc.toMap("partyIdFrom", buCode), UtilMisc.toList("-partyIdTo"), false);
//
//		    if (UtilValidate.isNotEmpty(buShipList)) {
//			for (GenericValue item : buShipList) {
//			    Map<String, Object> m = new HashMap<>();
//			    String userLoginId1 = item.getString("partyIdTo");
//
//			    List<GenericValue> userList = delegator.findByAnd("PersonAndUser",
//				    UtilMisc.toMap("userLoginId", userLoginId1), null, false);
//			    if (UtilValidate.isNotEmpty(userList)) {
//				GenericValue person = EntityUtil.getFirst(userList);
//				String name = person.getString("name");
//				if (UtilValidate.isEmpty(name)) {
//				    name = userLoginId1;
//				}
//				m.put("label", name);
//				m.put("value", userLoginId1);
//
//				if (!buPersonList3.contains(m)) {
//				    buPersonList3.add(m);
//				}
//				if (buCode.equals(buCode2)) {
//				    if (!buPersonList.contains(m)) {
//					buPersonList.add(m);
//				    }
//				}
//			    }
//			}
//		    }
//		}
//	    }

	    // 普通员工
	    if (isEmployee == true) {
		List<Map<String, Object>> list = FastList.newInstance();

		Map<String, Object> map1 = FastMap.newInstance();
		map1.put("label", "本人");
		map1.put("value", "nowData");
		map1.put("default", "Y");
		list.add(map1);

//		// 判断本大区是否已被禁用
//		boolean isShowBu = isShow(delegator, userLoginId, "N");
//		if (isShowBu) {
//		    Map<String, Object> map2 = FastMap.newInstance();
//		    map2.put("label", getBuName(delegator, userLoginId, "N"));
//		    map2.put("value", getBu(delegator, userLoginId, "N"));
//		    map2.put("default", "Y");
//		    list.add(map2);
//		}
		result.put("list", list);
	    }
	    // bu管理
	    if (isBu == true) {
		List<Map<String, Object>> list = FastList.newInstance();
		// 全国
		Map<String, Object> map5 = FastMap.newInstance();
		map5.put("label", "全国");
		map5.put("value", "allData");
		list.add(map5);
		// 判断本大区是否已被禁用
		boolean isShowBu = isShow(delegator, userLoginId, "N");
		if (isShowBu) {
		    Map<String, Object> map2 = FastMap.newInstance();
		    map2.put("label", getBuName(delegator, userLoginId, "N"));
		    map2.put("value", getBu(delegator, userLoginId, "N"));
		    map2.put("default", "Y");
		    list.add(map2);
		}

		// 担任bu经理的bu列表
		List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
			UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
			UtilMisc.toList("sequenceId"), false);
		int i = 0;
		x: for (GenericValue bu : dataList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    i = i + 1;
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", bu.getString("description"));
		    map4.put("value", bu.getString("dictionaryCode"));
		    if (!isShowBu && i == 1) {
			map4.put("default", "Y");
		    }
		    list.add(map4);
		}
		list = delRepeat(list);
		result.put("list", list);
	    }

	    // 大区管理
	    if (isArea == true) {
		List<Map<String, Object>> list = FastList.newInstance();
		// 全国
		Map<String, Object> map5 = FastMap.newInstance();
		map5.put("label", "全国");
		map5.put("value", "allData");
		list.add(map5);

		// 判断本大区是否已被禁用
		boolean isShowArea = isShow(delegator, userLoginId, "Y");
		if (isShowArea) {
		    Map<String, Object> map3 = FastMap.newInstance();
		    map3.put("label", getBuName(delegator, userLoginId, "Y"));
		    map3.put("value", getBu(delegator, userLoginId, "Y"));
		    map3.put("default", "Y");
		    list.add(map3);
		}
		// 担任大区经理的大区列表
		List<GenericValue> dataList1 = delegator.findByAnd("DataDictionary",
			UtilMisc.toMap("dictionaryTypeId", "AREA_DEPARTMENT", "dictionaryCode3", userLoginId),
			UtilMisc.toList("sequenceId"), false);
		int i = 0;
		x: for (GenericValue area : dataList1) {
		    String dictionaryCode2 = area.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    i = i + 1;
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", area.getString("description"));
		    map4.put("value", area.getString("dictionaryCode"));
		    if (!isShowArea && i == 1) {
			map4.put("default", "Y");
		    }

		    list.add(map4);
		}

		if (isBu == true) {
		    // 判断本大区是否已被禁用
		    boolean isShowBu = isShow(delegator, userLoginId, "N");
		    if (isShowBu) {
			Map<String, Object> map2 = FastMap.newInstance();
			map2.put("label", getBuName(delegator, userLoginId, "N"));
			map2.put("value", getBu(delegator, userLoginId, "N"));
			list.add(map2);
		    }
		    // 担任bu经理的bu列表
		    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
			    UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "dictionaryCode3", userLoginId),
			    UtilMisc.toList("sequenceId"), false);
		    int z = 0;
		    x: for (GenericValue bu : dataList) {
			z = z + 1;
			String dictionaryCode2 = bu.getString("dictionaryCode2");
			if ("N".equals(dictionaryCode2)) {
			    continue x;
			}
			Map<String, Object> map4 = FastMap.newInstance();
			map4.put("label", bu.getString("description"));
			map4.put("value", bu.getString("dictionaryCode"));
			list.add(map4);
		    }
		}

		// 本大区下的各个bu
		if (UtilValidate.isNotEmpty(shipList)) {
		    String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
		    if (UtilValidate.isNotEmpty(buCode)) {
			List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
				UtilMisc.toMap("dictionaryCode", buCode), UtilMisc.toList("sequenceId"), false);
			GenericValue data = EntityUtil.getFirst(dataList);
			List<GenericValue> sunBuList = delegator
				.findByAnd(
					"DataDictionary", UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT",
						"parentId", data.getString("parentId")),
					UtilMisc.toList("sequenceId"), false);
			x: for (GenericValue bu : sunBuList) {
			    String dictionaryCode2 = bu.getString("dictionaryCode2");
			    if ("N".equals(dictionaryCode2)) {
				continue x;
			    }
			    Map<String, Object> map4 = FastMap.newInstance();
			    map4.put("label", bu.getString("description"));
			    map4.put("value", bu.getString("dictionaryCode"));
			    list.add(map4);
			}
		    }
		}
		list = delRepeat(list);
		result.put("list", list);
	    }

	    // 管理者
	    if (isManage == true) {
		List<Map<String, Object>> list = FastList.newInstance();

		Map<String, Object> map3 = FastMap.newInstance();
		map3.put("label", "全国");
		map3.put("value", "allData");
		map3.put("default", "Y");
		list.add(map3);

		if (isArea == true) {
		    // 判断本大区是否已被禁用
		    boolean isShowArea = isShow(delegator, userLoginId, "Y");
		    if (isShowArea) {
			Map<String, Object> map31 = FastMap.newInstance();
			map31.put("label", getBuName(delegator, userLoginId, "Y"));
			map31.put("value", getBu(delegator, userLoginId, "Y"));
			list.add(map31);
		    }
		}
		// 各大区
		x: for (GenericValue area : areaList) {
		    String dictionaryCode2 = area.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", area.getString("description"));
		    map4.put("value", area.getString("dictionaryCode"));
		    list.add(map4);
		}

		if (isBu == true) {
		    // 判断本大区是否已被禁用
		    boolean isShowBu = isShow(delegator, userLoginId, "N");
		    if (isShowBu) {
			Map<String, Object> map2 = FastMap.newInstance();
			map2.put("label", getBuName(delegator, userLoginId, "N"));
			map2.put("value", getBu(delegator, userLoginId, "N"));
			list.add(map2);
		    }
		}

		// 各bu
		x: for (GenericValue bu : buList) {
		    String dictionaryCode2 = bu.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode2)) {
			continue x;
		    }
		    Map<String, Object> map4 = FastMap.newInstance();
		    map4.put("label", bu.getString("description"));
		    map4.put("value", bu.getString("dictionaryCode"));
		    list.add(map4);
		}
		list = delRepeat(list);
		result.put("list", list);
	    }
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError(e.getMessage());
	}
	return result;
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
	Map<Object, Boolean> seen = new ConcurrentHashMap<>();
	return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    public static List<Map<String, Object>> delRepeat(List<Map<String, Object>> list) {
	List<Map<String, Object>> myList = list.stream().distinct().collect(Collectors.toList());
	// 二次去重.根据值去重
	List<Map<String, Object>> newList = FastList.newInstance();
	// 二次去重
	myList = myList.stream().filter(distinctByKey(o -> o.get("value"))).collect(Collectors.toList());
	int i = 0;
	for (Map<String, Object> map : myList) {
	    if (UtilValidate.isEmpty(map.get("value"))) {
		map.put("value", i + "test");
	    }
	    newList.add(map);
	}
	return newList;
    }

    public static boolean isShow(Delegator delegator, String userLoginId, String isArea) throws GenericEntityException {
	boolean isShow = true;
	List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
		UtilMisc.toMap("partyIdTo", userLoginId), null, false);

	if (UtilValidate.isNotEmpty(itemShipList)) {
	    String buCode = EntityUtil.getFirst(itemShipList).getString("partyIdFrom");
	    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryCode", buCode), UtilMisc.toList("sequenceId"), true);

	    GenericValue data = EntityUtil.getFirst(dataList);
	    String dictionaryCode2 = data.getString("dictionaryCode2");
	    if (isArea.equals("N")) {
		if ("N".equals(dictionaryCode2)) {
		    isShow = false;
		}
	    } else {
		String areaId = data.getString("parentId");
		if (UtilValidate.isNotEmpty(areaId)) {
		    GenericValue areaData = delegator.findOne("DataDictionary", true,
			    UtilMisc.toMap("dictionaryId", areaId));
		    String dictionaryCode22 = areaData.getString("dictionaryCode2");
		    if ("N".equals(dictionaryCode22)) {
			isShow = false;
		    }
		} else {
		    isShow = false;
		}
	    }
	}

	return isShow;
    }

    public static String getBu(Delegator delegator, String userLoginId, String isArea) throws GenericEntityException {
	String id = "";
	List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
		UtilMisc.toMap("partyIdTo", userLoginId), null, false);

	if (UtilValidate.isNotEmpty(itemShipList)) {
	    String buCode = EntityUtil.getFirst(itemShipList).getString("partyIdFrom");
	    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryCode", buCode), UtilMisc.toList("sequenceId"), true);
	    GenericValue data = EntityUtil.getFirst(dataList);
	    if (isArea.equals("N")) {
		id = buCode;
	    } else {
		String areaId = data.getString("parentId");
		if (UtilValidate.isNotEmpty(areaId)) {
		    GenericValue areaData = delegator.findOne("DataDictionary", true,
			    UtilMisc.toMap("dictionaryId", areaId));
		    String dictionaryCode11 = areaData.getString("dictionaryCode");
		    id = dictionaryCode11;
		}
	    }
	}
	return id;
    }

    public static String getBuName(Delegator delegator, String userLoginId, String isArea)
	    throws GenericEntityException {
	String name = "";
	List<GenericValue> itemShipList = delegator.findByAnd("CoRelationship",
		UtilMisc.toMap("partyIdTo", userLoginId), null, false);

	if (UtilValidate.isNotEmpty(itemShipList)) {
	    String buCode = EntityUtil.getFirst(itemShipList).getString("partyIdFrom");
	    List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryCode", buCode), UtilMisc.toList("sequenceId"), true);
	    GenericValue data = EntityUtil.getFirst(dataList);
	    if (isArea.equals("N")) {
		name = data.getString("description");
	    } else {
		String areaId = data.getString("parentId");
		if (UtilValidate.isNotEmpty(areaId)) {
		    GenericValue areaData = delegator.findOne("DataDictionary", true,
			    UtilMisc.toMap("dictionaryId", areaId));
//		    String dictionaryCode11 = areaData.getString("dictionaryCode");
		    name = areaData.getString("description");
		}
	    }
	}
	return name;
    }

    /*
     * 批量保存权限组的权限
     */
    public static Map<String, Object> saveSysSecurityPermissionToSecurityGroup(DispatchContext ctx,
	    Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Debug.log("saveSysSecurityPermissionToSecurityGroup   === start");
	Delegator delegator = ctx.getDelegator();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	LocalDispatcher dispatcher = ctx.getDispatcher();
	// Security security = ctx.getSecurity();
	try {
	    String groupId = (String) context.get("groupId");

	    // GenericValue group = delegator.findOne("SecurityGroup",
	    // UtilMisc.toMap("groupId", groupId), false);

	    String permissionIdListString = (String) context.get("permissionIdList");
	    Debug.log("groupId = " + groupId);
	    Debug.log("permissionIdListString = " + permissionIdListString);
	    String[] permissionIdList = {};
	    if (UtilValidate.isNotEmpty(context.get("permissionIdList"))) {
		permissionIdList = permissionIdListString.split(",");
	    }

	    List<String> addPermissionIdList = FastList.newInstance();
	    for (int i = 0; i < permissionIdList.length; i++) {
		if (UtilValidate.isNotEmpty(permissionIdList[i])) {
		    String newPermissionId = permissionIdList[i];
		    if (!addPermissionIdList.contains(newPermissionId)) {
			addPermissionIdList.add(newPermissionId);
		    }
		}
	    }

	    List<GenericValue> groupList = delegator.findByAnd("SecurityGroupPermission",
		    UtilMisc.toMap("groupId", groupId), null, false);
	    if (UtilValidate.isNotEmpty(groupList)) {
		delegator.removeAll(groupList);
	    }

	    Debug.log("addPermissionId = " + addPermissionIdList);
	    for (String addPermissionId : addPermissionIdList) {
		Debug.log("addPermissionId = " + addPermissionId);
		if (UtilValidate.isNotEmpty(addPermissionId)) {
		    dispatcher.runSync("addSecurityPermissionToSecurityGroup", UtilMisc.toMap("userLogin", userLogin,
			    "groupId", groupId, "permissionId", addPermissionId));
		}
	    }
	} catch (GenericServiceException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("批量新增和删除权限" + e.getMessage());
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("批量新增和删除权限" + e.getMessage());
	}
	Debug.log("saveSysSecurityPermissionToSecurityGroup   === end");
	return result;
    }

//	/*
//	 * 
//	 */
//	public static Map<String, Object> saveEcSecurityPermissionToSecurityGroup(DispatchContext ctx, Map<String, Object> context) {
//        Map<String, Object> result = ServiceUtil.returnSuccess();
//        Debug.log("saveEcSecurityPermissionToSecurityGroup   === start");
//        Delegator delegator = ctx.getDelegator();
//		GenericValue userLogin = (GenericValue) context.get("userLogin");
//		LocalDispatcher dispatcher = ctx.getDispatcher();
//		Security security = ctx.getSecurity();
//        try {
//            String groupId = (String) context.get("groupId");
//            
//            GenericValue group = delegator.findOne("SecurityGroup", UtilMisc.toMap("groupId", groupId), false);
//            
//            List<GenericValue> shipList = delegator.findByAnd("PartyRelationship", 
//            		UtilMisc.toMap("partyIdTo", userLogin.getString("partyId"), "roleTypeIdFrom", "INTERNAL_ORGANIZATIO"
//            		, "roleTypeIdTo", "EMPLOYEE", "partyRelationshipTypeId", "EMPLOYMENT"), null, false); 
//            if(UtilValidate.isNotEmpty(shipList)){
//            	GenericValue ship = EntityUtil.getFirst(shipList);
//            	String companyId = ship.getString("partyIdFrom");
//            	String isView = "N";
//            	if(UtilValidate.isNotEmpty(group.get("ownerPartyId"))){
//            		if(group.getString("ownerPartyId").equals(companyId)){
//                		isView = "Y";
//                	}
//            	}
//        	    if(isView.equals("N")){
//        	    	Debug.logError("您没有权限对这个角色做权限操作!", module);
//       			    return ServiceUtil.returnError("您没有权限对这个角色做权限操作!");
//        	    }
//            }
//            
//            String permissionIdListString = (String) context.get("permissionIdList");
//            Debug.log("groupId = "+groupId);
//            Debug.log("permissionIdListString = "+permissionIdListString);
//            String[] permissionIdList={};
//            if(UtilValidate.isNotEmpty(context.get("permissionIdList"))){
//                permissionIdList = permissionIdListString.split(",");
//            }
//            
//            List<String> addPermissionIdList=FastList.newInstance();
//            for(int i=0;i<permissionIdList.length;i++){
//                if(UtilValidate.isNotEmpty(permissionIdList[i])){
//                	String newPermissionId =permissionIdList[i];
//                	if(!addPermissionIdList.contains(newPermissionId)){
//                        addPermissionIdList.add(newPermissionId);
//                    }
//                }
//            }
//           
//            List<GenericValue> groupList = delegator.findByAnd("SecurityGroupPermission", UtilMisc.toMap("groupId", groupId), null, false);
//            if(UtilValidate.isNotEmpty(groupList)){
//            	delegator.removeAll(groupList);
//            }
//           
//            Debug.log("addPermissionId = "+addPermissionIdList);
//            for(String addPermissionId:addPermissionIdList){
//            	Debug.log("addPermissionId = "+addPermissionId);
//            	if(UtilValidate.isNotEmpty(addPermissionId)){
//            		Map<String, Object> runSync = dispatcher.runSync("addSecurityPermissionToSecurityGroup", UtilMisc.toMap("userLogin",userLogin,"groupId", groupId,"permissionId", addPermissionId));
//            	}
//            }
//        } 
//        catch (GenericServiceException e) {
//			Debug.logError(e.getMessage(), module);
//			 return ServiceUtil.returnError("批量新增和删除权限"+e.getMessage());
//		} catch (GenericEntityException e) {
//			Debug.logError(e.getMessage(), module);
//			return ServiceUtil.returnError("批量新增和删除权限"+e.getMessage());
//		}
//        Debug.log("saveEcSecurityPermissionToSecurityGroup   === end");
//        return result;
//    }

    /**
     * 给用户同时分配多个权限组
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> saveSysSecurityGroupToUser(DispatchContext ctx, Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Debug.log("saveEcSecurityGroupToParty   === start");
	Delegator delegator = ctx.getDelegator();
//	LocalDispatcher dispatcher = ctx.getDispatcher();
//	GenericValue userLogin = (GenericValue) context.get("userLogin");
	try {
	    String partyId = (String) context.get("partyId");

	    List<GenericValue> userLoginList = delegator.findByAnd("UserLogin", UtilMisc.toMap("partyId", partyId),
		    UtilMisc.toList("userLoginId"), true);
	    Debug.log("userLoginList ====  " + userLoginList);
	    List<String> assocSmartUserIdList = EntityUtil.getFieldListFromEntityList(userLoginList, "userLoginId",
		    true);
	    Debug.log("assocSmartUserIdList ====  " + assocSmartUserIdList);

	    List<String> groupIdList = null;
	    if (UtilValidate.isNotEmpty(context.get("groupIdList"))) {
		groupIdList = splitCustomStringToList((String) context.get("groupIdList"), ",");
	    }
	    /*
	     * if(!groupIdList.contains("ofbizManagement")){
	     * groupIdList.add("ofbizManagement"); }
	     */
	    Timestamp now = UtilDateTime.nowTimestamp();
	    List<GenericValue> addGroupList = FastList.newInstance(), removeGroupList = FastList.newInstance();
	    for (String userId : assocSmartUserIdList) {
		List<String> addGroupIdList = FastList.newInstance();
		List<String> removeGroupIdList = FastList.newInstance();

		List<GenericValue> oldUserSecurityGroupList = FastList.newInstance();

		oldUserSecurityGroupList.addAll(delegator.findByAnd("UserLoginSecurityGroup",
			UtilMisc.toMap("userLoginId", userId), UtilMisc.toList("-fromDate"), false));

		oldUserSecurityGroupList = EntityUtil.filterByDate(oldUserSecurityGroupList);
		List<String> oldUserSecurityGroupIdList = EntityUtil
			.getFieldListFromEntityList(oldUserSecurityGroupList, "groupId", true);

		for (String inGroupId : groupIdList) {
		    if (!oldUserSecurityGroupIdList.contains(inGroupId)) {
			GenericValue userGroup = delegator.makeValue("UserLoginSecurityGroup",
				UtilMisc.toMap("userLoginId", userId, "groupId", inGroupId, "fromDate", now));
			addGroupList.add(userGroup);
			addGroupIdList.add(inGroupId);
		    }
		}

		for (String oldGroupId : oldUserSecurityGroupIdList) {
		    if (!groupIdList.contains(oldGroupId)) {
			removeGroupIdList.add(oldGroupId);
		    }
		}

		for (GenericValue oldGroup : oldUserSecurityGroupList) {
		    if (removeGroupIdList.contains(oldGroup.getString("groupId"))) {
			// 设置过期, 然后存储, 不删除
			oldGroup.set("thruDate", now);
			oldGroup.set("removed", "Y");
			removeGroupList.add(oldGroup);
		    }
		}
	    }
	    addGroupList = addGroupList.stream().distinct().collect(Collectors.toList());
	    removeGroupList = removeGroupList.stream().distinct().collect(Collectors.toList());

	    delegator.storeAll(addGroupList);
	    delegator.storeAll(removeGroupList);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("批量新增和删除用户权限组失败: " + e.getMessage());
	}
	Debug.log("saveEcSecurityGroupToParty   === end");
	return result;
    }

    /**
     * 权限组新增登陆账号
     */
    public static Map<String, Object> addSysUserToSecurityGroup(DispatchContext ctx, Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Debug.log("saveEcSecurityGroupToParty   === start");
	Delegator delegator = ctx.getDelegator();
//	LocalDispatcher dispatcher = ctx.getDispatcher();
//	GenericValue userLogin = (GenericValue) context.get("userLogin");
	try {
	    String partyId = (String) context.get("partyId");
	    String groupId = (String) context.get("groupId");

	    List<GenericValue> userLoginList = delegator.findByAnd("UserLogin", UtilMisc.toMap("partyId", partyId),
		    UtilMisc.toList("userLoginId"), true);
	    Debug.log("userLoginList ====  " + userLoginList);
	    List<String> assocSmartUserIdList = EntityUtil.getFieldListFromEntityList(userLoginList, "userLoginId",
		    true);
	    Debug.log("assocSmartUserIdList ====  " + assocSmartUserIdList);

	    Timestamp now = UtilDateTime.nowTimestamp();
	    List<GenericValue> addGroupList = FastList.newInstance();
	    for (String userId : assocSmartUserIdList) {

		GenericValue userGroup = delegator.makeValue("UserLoginSecurityGroup",
			UtilMisc.toMap("userLoginId", userId, "groupId", groupId, "fromDate", now));
		addGroupList.add(userGroup);
	    }
	    delegator.storeAll(addGroupList);

	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("权限组新增用户: " + e.getMessage());
	}
	Debug.log("addSysUserToSecurityGroup   === end");
	return result;
    }

    /**
     * 权限组删除登陆账号
     */
    public static Map<String, Object> removeSysUserToSecurityGroup(DispatchContext ctx, Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Debug.log("saveEcSecurityGroupToParty   === start");
	Delegator delegator = ctx.getDelegator();
//	LocalDispatcher dispatcher = ctx.getDispatcher();
//	GenericValue userLogin = (GenericValue) context.get("userLogin");
	try {
	    String partyId = (String) context.get("partyId");
	    String groupId = (String) context.get("groupId");

	    List<GenericValue> userLoginList = delegator.findByAnd("UserLogin", UtilMisc.toMap("partyId", partyId),
		    UtilMisc.toList("userLoginId"), true);
	    Debug.log("userLoginList ====  " + userLoginList);
	    List<String> assocSmartUserIdList = EntityUtil.getFieldListFromEntityList(userLoginList, "userLoginId",
		    true);
	    Debug.log("assocSmartUserIdList ====  " + assocSmartUserIdList);

//	    Timestamp now = UtilDateTime.nowTimestamp();
	    for (String userId : assocSmartUserIdList) {
		delegator.removeByAnd("UserLoginSecurityGroup",
			UtilMisc.toMap("userLoginId", userId, "groupId", groupId));
	    }

	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("权限组删除用户: " + e.getMessage());
	}
	Debug.log("removeSysUserToSecurityGroup   === end");
	return result;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static List splitCustomStringToList(String inputString, String splitChar) {
	List result = FastList.newInstance();
	if (UtilValidate.isEmpty(inputString)) {
	    return result;
	}
	inputString = inputString.trim();
	String[] array = inputString.split(splitChar);
	for (int i = 0; i < array.length; i++) {
	    if (UtilValidate.isNotEmpty(array[i])) {
		result.add(array[i]);
	    }
	}
	return result;
    }

    /*
     * 批量保存权限组的权限
     */
    public static Map<String, Object> saveCoSecurityPermissionToSecurityGroup(DispatchContext ctx,
	    Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Debug.log("saveSysSecurityPermissionToSecurityGroup   === start");
	Delegator delegator = ctx.getDelegator();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	LocalDispatcher dispatcher = ctx.getDispatcher();
	// Security security = ctx.getSecurity();
	try {
	    String groupId = (String) context.get("groupId");

	    // GenericValue group = delegator.findOne("SecurityGroup",
	    // UtilMisc.toMap("groupId", groupId), false);

	    String permissionIdListString = (String) context.get("permissionIdList");
	    Debug.log("groupId = " + groupId);
	    Debug.log("permissionIdListString = " + permissionIdListString);
	    String[] permissionIdList = {};
	    if (UtilValidate.isNotEmpty(context.get("permissionIdList"))) {
		permissionIdList = permissionIdListString.split(",");
	    }

	    List<String> addPermissionIdList = FastList.newInstance();
	    for (int i = 0; i < permissionIdList.length; i++) {
		if (UtilValidate.isNotEmpty(permissionIdList[i])) {
		    String newPermissionId = permissionIdList[i];
		    if (!addPermissionIdList.contains(newPermissionId)) {
			addPermissionIdList.add(newPermissionId);
		    }
		}
	    }

	    List<GenericValue> groupList = delegator.findByAnd("SecurityGroupPermission",
		    UtilMisc.toMap("groupId", groupId), null, false);
	    if (UtilValidate.isNotEmpty(groupList)) {
		delegator.removeAll(groupList);
	    }

	    Debug.log("addPermissionId = " + addPermissionIdList);
	    for (String addPermissionId : addPermissionIdList) {
		Debug.log("addPermissionId = " + addPermissionId);
		if (UtilValidate.isNotEmpty(addPermissionId)) {
		    dispatcher.runSync("addSecurityPermissionToSecurityGroup", UtilMisc.toMap("userLogin", userLogin,
			    "groupId", groupId, "permissionId", addPermissionId));
		}
	    }
	} catch (GenericServiceException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("批量新增和删除权限" + e.getMessage());
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("批量新增和删除权限" + e.getMessage());
	}
	Debug.log("saveSysSecurityPermissionToSecurityGroup   === end");
	return result;
    }

    /**
     * 给用户同时分配多个权限组
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> saveCoSecurityGroupToUser(DispatchContext ctx, Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Debug.log("saveEcSecurityGroupToParty   === start");
	Delegator delegator = ctx.getDelegator();
//	LocalDispatcher dispatcher = ctx.getDispatcher();
//	GenericValue userLogin = (GenericValue) context.get("userLogin");
	try {
	    String partyId = (String) context.get("partyId");

	    List<GenericValue> userLoginList = delegator.findByAnd("UserLogin", UtilMisc.toMap("partyId", partyId),
		    UtilMisc.toList("userLoginId"), true);
	    Debug.log("userLoginList ====  " + userLoginList);
	    List<String> assocSmartUserIdList = EntityUtil.getFieldListFromEntityList(userLoginList, "userLoginId",
		    true);
	    Debug.log("assocSmartUserIdList ====  " + assocSmartUserIdList);

	    List<String> groupIdList = null;
	    if (UtilValidate.isNotEmpty(context.get("groupIdList"))) {
		groupIdList = splitCustomStringToList((String) context.get("groupIdList"), ",");
	    }
	    /*
	     * if(!groupIdList.contains("ofbizManagement")){
	     * groupIdList.add("ofbizManagement"); }
	     */
	    Timestamp now = UtilDateTime.nowTimestamp();
	    List<GenericValue> addGroupList = FastList.newInstance(), removeGroupList = FastList.newInstance();
	    for (String userId : assocSmartUserIdList) {
		List<String> addGroupIdList = FastList.newInstance();
		List<String> removeGroupIdList = FastList.newInstance();

		List<GenericValue> oldUserSecurityGroupList = FastList.newInstance();

		oldUserSecurityGroupList.addAll(delegator.findByAnd("UserLoginSecurityGroup",
			UtilMisc.toMap("userLoginId", userId), UtilMisc.toList("-fromDate"), false));

		oldUserSecurityGroupList = EntityUtil.filterByDate(oldUserSecurityGroupList);
		List<String> oldUserSecurityGroupIdList = EntityUtil
			.getFieldListFromEntityList(oldUserSecurityGroupList, "groupId", true);

		for (String inGroupId : groupIdList) {
		    if (!oldUserSecurityGroupIdList.contains(inGroupId)) {
			GenericValue userGroup = delegator.makeValue("UserLoginSecurityGroup",
				UtilMisc.toMap("userLoginId", userId, "groupId", inGroupId, "fromDate", now));
			addGroupList.add(userGroup);
			addGroupIdList.add(inGroupId);
		    }
		}

		for (String oldGroupId : oldUserSecurityGroupIdList) {
		    if (!groupIdList.contains(oldGroupId)) {
			removeGroupIdList.add(oldGroupId);
		    }
		}

		for (GenericValue oldGroup : oldUserSecurityGroupList) {
		    if (removeGroupIdList.contains(oldGroup.getString("groupId"))) {
			// 设置过期, 然后存储, 不删除
			oldGroup.set("thruDate", now);
			removeGroupList.add(oldGroup);
		    }
		}
	    }
	    delegator.storeAll(addGroupList);
	    delegator.storeAll(removeGroupList);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("批量新增和删除用户权限组失败: " + e.getMessage());
	}
	Debug.log("saveEcSecurityGroupToParty   === end");
	return result;
    }

    /**
     * 权限组新增登陆账号
     */
    public static Map<String, Object> addCoUserToSecurityGroup(DispatchContext ctx, Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Debug.log("saveEcSecurityGroupToParty   === start");
	Delegator delegator = ctx.getDelegator();
//	LocalDispatcher dispatcher = ctx.getDispatcher();
//	GenericValue userLogin = (GenericValue) context.get("userLogin");
	try {
	    String partyId = (String) context.get("partyId");
	    String groupId = (String) context.get("groupId");

	    List<GenericValue> userLoginList = delegator.findByAnd("UserLogin", UtilMisc.toMap("partyId", partyId),
		    UtilMisc.toList("userLoginId"), true);
	    Debug.log("userLoginList ====  " + userLoginList);
	    List<String> assocSmartUserIdList = EntityUtil.getFieldListFromEntityList(userLoginList, "userLoginId",
		    true);
	    Debug.log("assocSmartUserIdList ====  " + assocSmartUserIdList);

	    Timestamp now = UtilDateTime.nowTimestamp();
	    List<GenericValue> addGroupList = FastList.newInstance();
	    for (String userId : assocSmartUserIdList) {

		GenericValue userGroup = delegator.makeValue("UserLoginSecurityGroup",
			UtilMisc.toMap("userLoginId", userId, "groupId", groupId, "fromDate", now));
		addGroupList.add(userGroup);
	    }
	    delegator.storeAll(addGroupList);

	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("权限组新增用户: " + e.getMessage());
	}
	Debug.log("addSysUserToSecurityGroup   === end");
	return result;
    }

    /**
     * 权限组删除登陆账号
     */
    public static Map<String, Object> removeCoUserToSecurityGroup(DispatchContext ctx, Map<String, Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Debug.log("saveEcSecurityGroupToParty   === start");
	Delegator delegator = ctx.getDelegator();
//	LocalDispatcher dispatcher = ctx.getDispatcher();
//	GenericValue userLogin = (GenericValue) context.get("userLogin");
	try {
	    String partyId = (String) context.get("partyId");
	    String groupId = (String) context.get("groupId");

	    List<GenericValue> userLoginList = delegator.findByAnd("UserLogin", UtilMisc.toMap("partyId", partyId),
		    UtilMisc.toList("userLoginId"), true);
	    Debug.log("userLoginList ====  " + userLoginList);
	    List<String> assocSmartUserIdList = EntityUtil.getFieldListFromEntityList(userLoginList, "userLoginId",
		    true);
	    Debug.log("assocSmartUserIdList ====  " + assocSmartUserIdList);

//	    Timestamp now = UtilDateTime.nowTimestamp();
	    for (String userId : assocSmartUserIdList) {
		delegator.removeByAnd("UserLoginSecurityGroup",
			UtilMisc.toMap("userLoginId", userId, "groupId", groupId));
	    }

	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("权限组删除用户: " + e.getMessage());
	}
	Debug.log("removeSysUserToSecurityGroup   === end");
	return result;
    }

}
